package com.ktjiaoyu.crm.controller;

import com.ktjiaoyu.crm.pojo.User;
import com.ktjiaoyu.crm.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@Slf4j
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 去到登录页面
     * @return
     */
    @RequestMapping(value = "/toLogin")
    public String toLogin(){

        return "login";
    }

    @RequestMapping(value = "/main")
    public String main(){

        return "main";
    }

    @RequestMapping(value = "/403")
    public String unantherzier(){

        return "403";
    }

    /**
     * 登录
     * @param usrName
     * @param usrPassword
     * @param model
     * @param session
     * @return
     */
    @RequestMapping(value = "/login")
    public String login(String usrName, String usrPassword, Model model, HttpSession session)throws Exception{
           /*User user = userService.login(usrName,usrPassword);
           if(user!=null){
               session.setAttribute("loginUser",user);
               return "redirect:/main";
           }
           model.addAttribute("msg","用户名或密码错误，登录失败！");

           return "forward:/toLogin";*/

        try {
            log.info("{}用户进入登录方法........",usrName);
            //构建用户token
            UsernamePasswordToken token = new UsernamePasswordToken(usrName,usrPassword);
            //获取Subject
            Subject subject = SecurityUtils.getSubject();
            //调用Shiro登录方法
            subject.login(token);
            //获取返回的用户信息，如果不为空，说明登录成功，跳转到主页
            User user = (User) subject.getPrincipal();
            //将用户信息存入session
            session.setAttribute("loginUser",user);
            log.info("{}认证成功............",usrName);
            return "redirect:/main";
        } catch (UnknownAccountException | IncorrectCredentialsException e){
            log.error("异常原因：{}用户名或密码错误，登录失败！",usrName);
            model.addAttribute("msg","用户名或密码错误，登录失败！ ");
            return "forward:/toLogin";
        } catch (LockedAccountException e){
            log.error("异常原因：{}用户被禁用，登录失败！",usrName);
            model.addAttribute("msg","用户被禁用，登录失败！ ");
            return "forward:/toLogin";
        } catch (AuthenticationException e){
            log.error("异常原因：{}认证异常，登录失败！",usrName);
            model.addAttribute("msg","认证异常，登录失败！ ");
            return "forward:/toLogin";
        } catch (Exception e) {
            log.error("出现其他异常！异常原因：{}",e.getLocalizedMessage(),e);
            model.addAttribute("msg","出现其他异常！");
            e.printStackTrace();
            return "forward:/toLogin";
        }

    }

    /**
     * 注销
     * @param session
     * @return
     */
    @RequestMapping(value = "/logout")
    public String logout(HttpSession session){
        session.removeAttribute("loginUser");   //删除session中保存的用户信息
        SecurityUtils.getSubject().logout();    //Shiro注销
        return "redirect:/main";
    }


}
